<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/40">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Cacti/5. cacti监控找到网卡的方法/">cacti 监控找到网卡的方法</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Cacti/">Cacti</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只需要在客户机（要监控的机器）修改配置文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/snmp/snmpd.conf </span></div><div class="line"></div><div class="line"></div><div class="line">view    systemview    included   .1.3.6.1.2.1.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">view    systemview    included   .1.3.6.1.2.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后重启snmpd服务；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/snmpd restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在web监控里面添加</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E7%9B%91%E6%8E%A7%E6%89%BE%E5%88%B0%E7%BD%91%E5%8D%A1%E7%9A%84%E6%96%B9%E6%B3%95/01.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类型选择Graph Types: SNMP - Interface Statistics 就看到客户机的网卡了。点击create。保存。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E7%9B%91%E6%8E%A7%E6%89%BE%E5%88%B0%E7%BD%91%E5%8D%A1%E7%9A%84%E6%96%B9%E6%B3%95/02.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Graphs中就可以看到添加的网卡流量信息了；</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E7%9B%91%E6%8E%A7%E6%89%BE%E5%88%B0%E7%BD%91%E5%8D%A1%E7%9A%84%E6%96%B9%E6%B3%95/03.jpg?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Cacti/4. cacti 有图无数据参数设定/">cacti 有图无数据参数设定</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Cacti/">Cacti</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一切参数设定在客户端。（/etc/snmp/snmpd.conf）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#       sec.name  source          community</span></div><div class="line">com2sec notConfigUser  192.168.1.107(cacti主机的IP)       public</div><div class="line"></div><div class="line"><span class="comment">#       name           incl/excl     subtree         mask(optional)</span></div><div class="line"><span class="comment">#view    systemview    included   .1.3.6.1.2.1.1</span></div><div class="line">view    systemview    included   .1.3.6.1.2.1</div><div class="line">view    systemview    included   .1.3.6.1.2.1.25.1.1</div><div class="line">view all        include         .1</div><div class="line"></div><div class="line"><span class="comment">#       group          context sec.model sec.level prefix read   write  notif</span></div><div class="line">access  notConfigGroup <span class="string">""</span>      any       noauth    exact  all none none</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后重启客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service snmpd restart</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Cacti/3. cacti 的错误/">cacti的错误</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Cacti/">Cacti</a>
			</span>
		
	</div>

	

	
		<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">FATAL: Cannot connect to MySQL server on <span class="string">'localhost'</span>. Please make sure you have specified a valid MySQL database name <span class="keyword">in</span> <span class="string">'include/config.php'</span></div></pre></td></tr></table></figure>
<h2 id="1-查看config-php下，是否配置正确"><a href="#1-查看config-php下，是否配置正确" class="headerlink" title="1.查看config.php下，是否配置正确"></a>1.查看config.php下，是否配置正确</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看cactiusr的host是否有localhost 权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql -ucactiuser -h localhost -p</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看登录是否成功 ，如果登录正常则排除</p>
<h2 id="2-MYSQL权限问题"><a href="#2-MYSQL权限问题" class="headerlink" title="2.MYSQL权限问题"></a>2.MYSQL权限问题</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">SQL&gt;GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY <span class="string">'somepassword'</span>;</div></pre></td></tr></table></figure>
<h2 id="3-当mysql中的所有配置和cacti的config-php都正确，却还是出现该错误时，那就是mysql套接字的原因了"><a href="#3-当mysql中的所有配置和cacti的config-php都正确，却还是出现该错误时，那就是mysql套接字的原因了" class="headerlink" title="3.当mysql中的所有配置和cacti的config.php都正确，却还是出现该错误时，那就是mysql套接字的原因了~"></a>3.当mysql中的所有配置和cacti的config.php都正确，却还是出现该错误时，那就是mysql套接字的原因了~</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cacti会自动的去查找/var/lib/mysql/mysql.sock 该套接字，但是如果是源码安装的mysql，未指定套接字位置时，套接字的位置为/tmp/mysql.sock，由于cacti未找到/var/lib/mysql/mysql.sock，所以显示的是“Cannot connect to MySQL server on ‘localhost’”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决：做一个/tmp/mysql.sock到/var/lib/mysql/mysql.sock的软连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln -s  /tmp/mysql.sock  /var/lib/mysql/mysql.sock</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Cacti/2. cacti增加客户端监控/">cacti增加客户端监控</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Cacti/">Cacti</a>
			</span>
		
	</div>

	

	
		<h2 id="1-安装-snmp"><a href="#1-安装-snmp" class="headerlink" title="1.安装 snmp"></a>1.安装 snmp</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># yum install -y net-snmp</span></div></pre></td></tr></table></figure>
<h2 id="2-修改-snmp-conf"><a href="#2-修改-snmp-conf" class="headerlink" title="2.修改 snmp.conf"></a>2.修改 snmp.conf</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /etc/snmp/snmpd.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 syslocation 以及 syscontact ，其中 syslocation 可以写本机 ip，syscontact 写管理员邮箱。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3.启动 snmp</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># service snmpd start</span></div></pre></td></tr></table></figure>
<h2 id="4-cacti-增加监控"><a href="#4-cacti-增加监控" class="headerlink" title="4.cacti 增加监控"></a>4.cacti 增加监控</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;登录 cacti 管理后台，点控制台，再点设备，在右上角点 “add” </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;描述： 写本机 ip 或自定义一个名字</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主机名：hostname 写本机 ip</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设备模版：选择 Net-SNMP Device </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;采集线程数：可以选择 2-5</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SNMP版本：改为 version 2</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置完成点右下角的创建，然后点右上角的 “Create Graphs for this Device”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Graph Types:  选择SNMP - Interface Statistics </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在下面框中选择要监控的网卡，比如我选择eth0, 在最右侧小方块里打对勾，然后点右下角的create（如果在这一步找不到网卡，可以参考<a href="https://hcldirgit.github.io/2017/08/12/Cacti/5.%20cacti%E7%9B%91%E6%8E%A7%E6%89%BE%E5%88%B0%E7%BD%91%E5%8D%A1%E7%9A%84%E6%96%B9%E6%B3%95/" target="_blank" rel="external">cacti监控找到网卡的方法</a>）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Graph Types:  再选择 Graph Template Based</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在下面的框中，选择你要监控的项目，比如ucd/net - Load Average </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在右侧小方块中打对勾，然后点右下角的创建</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点击设备，选择新增加主机，选择  defaur tree 点 go</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;defaut tree 下面已经增加了添加的主机，图形一开始不会那么快出来，要等一会才可以</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Cacti/1. cacti 安装/">cacti 安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Cacti/">Cacti</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：cacti 需要 apache 和 php 的支持，在这里使用 yum 安装 lamp 环境。</p>
<h2 id="1-安装-epel"><a href="#1-安装-epel" class="headerlink" title="1.安装 epel"></a>1.安装 epel</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># yum install -y epel-release</span></div></pre></td></tr></table></figure>
<h2 id="2-安装-lamp"><a href="#2-安装-lamp" class="headerlink" title="2.安装 lamp"></a>2.安装 lamp</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># yum install -y httpd php php-mysql mysql mysql-server mysql-devel php-gd libjpeg libjpeg-devel libpng libpng-devel</span></div></pre></td></tr></table></figure>
<h2 id="3-安装-cacti"><a href="#3-安装-cacti" class="headerlink" title="3.安装 cacti"></a>3.安装 cacti</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]# yum install -y cacti net-snmp net-snmp-utils rrdtool</div></pre></td></tr></table></figure>
<h2 id="4-启动服务"><a href="#4-启动服务" class="headerlink" title="4.启动服务"></a>4.启动服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># /etc/init.d/httpd start</span></div><div class="line">[root@cacti ~]<span class="comment"># /etc/init.d/mysqld start</span></div><div class="line">[root@cacti ~]<span class="comment"># /etc/init.d/snmpd start</span></div></pre></td></tr></table></figure>
<h2 id="5-编辑-httpd-conf"><a href="#5-编辑-httpd-conf" class="headerlink" title="5.编辑 httpd.conf"></a>5.编辑 httpd.conf</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># vim /etc/httpd/conf.d/cacti.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把 “Deny from all” 改为 “Allow from all”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># /etc/init.d/httpd restart</span></div></pre></td></tr></table></figure>
<h2 id="6-导入数据创建-cacti-库"><a href="#6-导入数据创建-cacti-库" class="headerlink" title="6.导入数据创建 cacti 库"></a>6.导入数据创建 cacti 库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># mysql -uroot -e "create database cacti"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建 cacti 用户</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># mysql -uroot -e "grant all on cacti.* to 'cacti'@'127.0.0.1' identified by 'cacti';"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;导入 sql 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># mysql -uroot cacti &lt; /usr/share/doc/cacti-1.0.4/cacti.sql</span></div></pre></td></tr></table></figure>
<h2 id="7-编辑-cacti-配置文件"><a href="#7-编辑-cacti-配置文件" class="headerlink" title="7.编辑 cacti 配置文件"></a>7.编辑 cacti 配置文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># vim /usr/share/cacti/include/config.php</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$database_type</span> = <span class="string">"mysql"</span>;</div><div class="line"><span class="variable">$database_default</span> = <span class="string">"cacti"</span>;</div><div class="line"><span class="variable">$database_name</span> = <span class="string">"cacti"</span>;</div><div class="line"><span class="variable">$database_hostname</span> = <span class="string">"127.0.0.1"</span>;</div><div class="line"><span class="variable">$database_username</span> = <span class="string">"cacti"</span>;</div><div class="line"><span class="variable">$database_password</span> = <span class="string">"cacti"</span>;</div><div class="line"><span class="variable">$database_port</span> = <span class="string">"3306"</span>;</div><div class="line"><span class="variable">$database_ssl</span> = <span class="literal">false</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<h2 id="8-web-访问-cacti-并安装"><a href="#8-web-访问-cacti-并安装" class="headerlink" title="8.web 访问 cacti 并安装"></a>8.web 访问 cacti 并安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浏览器访问  <a href="http://ip/cacti/" target="_blank" rel="external">http://ip/cacti/</a></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出现错误</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># set time_zone = '+8:00';</span></div><div class="line">[root@cacti ~]<span class="comment"># mysql -u root mysql -p -e "GRANT SELECT ON mysql.time_zone_name TO cacti@localhost IDENTIFIED BY 'cacti';"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;继续报错</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># vim /etc/php.ini</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root mysql -p</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决问题</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;报错</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要升级 mysql 版本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更新 yum 源</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># yum update</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加 mysql5.6 yum 源</p>
<ul>
<li>centos6</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm</span></div></pre></td></tr></table></figure>
<ul>
<li>centos7</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后 yum 安装 mysql 就是 5.6 版本了</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;版本更换后</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;根据提示 修改 /etc/my.cnf 配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># vim /etc/my.cnf</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 mysql 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># /etc/init.d/mysqld restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;刷新网页</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;报错</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装php-posix</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># yum -y install php-process</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点击下一步</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择默认  new primary server 下一步</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下一步</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下一步</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择模版，并点完成</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;登录，这里初始用户名和密码都为 admin</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/18.png?raw=true" alt="18"><figcaption class="figure__caption">18</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;初次登录强制要求更改密码。注：新密码长度必须 8 位以上，必须包含大小写字母、特殊符号、数字。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/19.png?raw=true" alt="19"><figcaption class="figure__caption">19</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后进入监控页面</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/20.png?raw=true" alt="20"><figcaption class="figure__caption">20</figcaption></figure></p>
<h2 id="9-执行-poller-php-，生成图形，加入任务计划"><a href="#9-执行-poller-php-，生成图形，加入任务计划" class="headerlink" title="9.执行 poller.php ，生成图形，加入任务计划"></a>9.执行 poller.php ，生成图形，加入任务计划</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;登录有选择图形是没有显示的</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/21.png?raw=true" alt="21"><figcaption class="figure__caption">21</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># /usr/bin/php /usr/share/cacti/poller.php</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本时提示很多 warning</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># vim /etc/php.ini</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/22.png?raw=true" alt="22"><figcaption class="figure__caption">22</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/23.png?raw=true" alt="23"><figcaption class="figure__caption">23</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样再次执行就不会提示 warning 了</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/24.png?raw=true" alt="24"><figcaption class="figure__caption">24</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加 cron 任务计划</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@cacti ~]<span class="comment"># crontab -e</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">*/5 * * * * /usr/bin/php/usr/share/cacti/poller.php</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;浏览器出现图形</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/cacti%E5%AE%89%E8%A3%85/25.png?raw=true" alt="25"><figcaption class="figure__caption">25</figcaption></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/9. Apache Prefork、Worker和Event三种MPM分析/">Apache Prefork、Worker和Event三种MPM分析</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<h2 id="三种MPM介绍"><a href="#三种MPM介绍" class="headerlink" title="三种MPM介绍"></a>三种MPM介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Apache 2.X  支持插入式并行处理模块，称为多路处理模块（MPM）。在编译apache时必须选择也只能选择一个MPM，对类UNIX系统，有几个不同的MPM可供选择，它们会影响到apache的速度和可伸缩性。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Prefork MPM</strong> : 这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器，它的工作方式类似于Apache 1.3。它适合于没有线程安全库，需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM，这样若一个请求出现问题就不会影响到其他请求。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个MPM具有很强的自我调节能力，只需要很少的配置指令调整。最重要的是将<code>MaxClients</code>设置为一个足够大的数值以处理潜在的请求高峰，同时又不能太大，以致需要使用的内存超出物理内存的大小。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Worker MPM</strong> : 此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求，所以可以处理海量请求，而系统资源的开销小于基于进程的MPM。但是，它也使用了多进程，每个进程又有多个线程，以获得基于进程的MPM的稳定性。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建立。每个子进程可以建立ThreadsPerChild数量的服务线程和一个监听线程，该监听线程监听接入请求并将其传递给服务线程处理和应答。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>不管是Worker模式或是Prefork 模式，Apache总是试图保持一些备用的(spare)或者是空闲的子进程（空闲的服务线程池）用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Event MPM</strong> ：以上两种稳定的MPM方式在非常繁忙的服务器应用下都有些不足。尽管HTTP的Keepalive方式能减少TCP连接数量和网络负载，但是 Keepalive需要和服务进程或者线程绑定，这就导致一个繁忙的服务器会耗光所有的线程。 Event MPM是解决这个问题的一种新模型，它把服务进程从连接中分离出来。在服务器处理速度很快，同时具有非常高的点击率时，可用的线程数量就是关键的资源限 制，此时Event MPM方式是最有效的。一个以Worker MPM方式工作的繁忙服务器能够承受每秒好几万次的访问量（例如在大型新闻服务站点的高峰时），而Event MPM可以用来处理更高负载。值得注意的是，Event MPM不能在安全HTTP（HTTPS）访问下工作。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于Event 模式，apache给出了以下警告：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">This MPM is experimental, so it may or may not work as expected .</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这种MPM目前处于试验状态，他可能不能按照预期的那样工作。</p>
<h2 id="如何配置三种MPM"><a href="#如何配置三种MPM" class="headerlink" title="如何配置三种MPM"></a>如何配置三种MPM</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Prefork 是UNIX平台上默认的MPM，它所采用的预派生子进程方式也是apache 1.3中采用的模式。prefork 本身并没有使用到线程，2.0 版本使用它是为了与1.3版保持兼容性；另一方面，perfork用单独的子进程来处理不同的请示，之程之间是彼此独立的，这也使其成为最稳定的MPM之一 。</p>
<h3 id="如何查看当前安装的Apache-的三种MPM。"><a href="#如何查看当前安装的Apache-的三种MPM。" class="headerlink" title="如何查看当前安装的Apache 的三种MPM。"></a>如何查看当前安装的Apache 的三种MPM。</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost apache]<span class="comment"># httpd -l</span></div><div class="line">Compiled <span class="keyword">in</span> modules:</div><div class="line">  core.c</div><div class="line">  prefork.c</div><div class="line">  http_core.c</div><div class="line">  mod_so.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你看到perfork.c 则表示当前为perfork MPM模式。worker.c 则表示为 worker MPM模式。</p>
<h3 id="那么如何设置apache的MPM呢？"><a href="#那么如何设置apache的MPM呢？" class="headerlink" title="那么如何设置apache的MPM呢？"></a>那么如何设置apache的MPM呢？</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要的apache 配置安装的时候需要指定模式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.4.1]<span class="comment"># ./configure --prefix=/usr/local/apache2worker --enable-so --with-mpm=worker </span></div><div class="line">[root@localhost httpd-2.4.1]<span class="comment"># make</span></div><div class="line">[root@localhost httpd-2.4.1]<span class="comment"># make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定–with-mpm=NAME 选项指定MPM，NAME就是你想使用的MPM的名称。不指定模式的话，默认为Prefork MPM。 </p>
<h3 id="那么如何配置成Event-MPM？"><a href="#那么如何配置成Event-MPM？" class="headerlink" title="那么如何配置成Event MPM？"></a>那么如何配置成Event MPM？</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同我上面的方法一样，只需要在安装的时候加上以下参数： <code>--enable-nonportable-atomics=yes</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要注意的是Event MPM对于老的CPU可能是不支持的。</p>
<h2 id="三种MPM参数分析"><a href="#三种MPM参数分析" class="headerlink" title="三种MPM参数分析"></a>三种MPM参数分析</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不管你安装的是apache哪种MPM</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在安装完成之后打开…/apache/conf/extra/httpd-mpm.conf文件，找到如下配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># perfork MPM</span></div><div class="line">&lt;IfModule mpm_prefork_module&gt;</div><div class="line">StartServers 5</div><div class="line">MinSpareServers 5</div><div class="line">MaxSpareServers 10</div><div class="line">MaxRequestWorkers 250</div><div class="line">MaxConnectionsPerChild 0</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<ul>
<li>StartServers:　　数量的服务器进程开始</li>
<li>MinSpareServers:　　最小数量的服务器进程,保存备用</li>
<li>MaxSpareServers:　　最大数量的服务器进程,保存备用</li>
<li>MaxRequestWorkers:　　最大数量的服务器进程允许开始</li>
<li>MaxConnectionsPerChild:　　最大连接数的一个服务器进程服务</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;prefork 控制进程在最初建立“StartServers”个子进程后，为了满足MinSpareServers设置的需要创建一个进程，等待一秒钟，继续创建两 个，再等待一秒钟，继续创建四个……如此按指数级增加创建的进程数，最多达到每秒32个，直到满足MinSpareServers设置的值为止。这种模式 可以不必在请求到来时再产生新的进程，从而减小了系统开销以增加性能。MaxSpareServers设置了最大的空闲进程数，如果空闲进程数大于这个 值，Apache会自动kill掉一些多余进程。这个值不要设得过大，但如果设的值比MinSpareServers小，Apache会自动把其调整为 MinSpareServers+1。如果站点负载较大，可考虑同时加大MinSpareServers和MaxSpareServers。  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild”个请求后将自 动销毁。0意味着无限，即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求，但如果设成非零值也有两点重要的好处：</p>
<ol>
<li>可防止意外的内存泄漏。</li>
<li>在服务器负载下降的时侯会自动减少子进程数。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因此，可根据服务器的负载来调整这个值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>MaxRequestWorkers指令集同时将服务请求的数量上的限制。任何连接尝试在MaxRequestWorkerslimit将通常被排队，最多若干基于上ListenBacklog指令。</strong> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>在apache2.3.13以前的版本MaxRequestWorkers被称为MaxClients 。</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;（MaxClients是这些指令中最为重要的一个，设定的是 Apache可以同时处理的请求，是对Apache性能影响最大的参数。其缺省值150是远远不够的，如果请求总数已达到这个值（可通过ps -ef|grep http|wc -l来确认），那么后面的请求就要排队，直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。虽然理论上这个值越大，可以 处理的请求就越多，但Apache默认的限制不能大于256。）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># worker MPM </span></div><div class="line">&lt;IfModule mpm_worker_module&gt;</div><div class="line">StartServers 3</div><div class="line">MinSpareThreads 75</div><div class="line">MaxSpareThreads 250 </div><div class="line">ThreadsPerChild 25</div><div class="line">MaxRequestWorkers 400</div><div class="line">MaxConnectionsPerChild 0</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<ul>
<li>StartServers:　　初始数量的服务器进程开始</li>
<li>MinSpareThreads:　　最小数量的工作线程,保存备用</li>
<li>MaxSpareThreads:　　最大数量的工作线程,保存备用</li>
<li>ThreadsPerChild:　　固定数量的工作线程在每个服务器进程</li>
<li>MaxRequestWorkers:　　最大数量的工作线程</li>
<li>MaxConnectionsPerChild:　　最大连接数的一个服务器进程服务</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Worker 由主控制进程生成“StartServers”个子进程，每个子进程中包含固定的ThreadsPerChild线程数，各个线程独立地处理请求。同样， 为了不在请求到来时再生成线程，MinSpareThreads和MaxSpareThreads设置了最少和最多的空闲线程数；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而MaxRequestWorkers 设置了同时连入的clients最大总数。如果现有子进程中的线程总数不能满足负载，控制进程将派生新的子进程 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MinSpareThreads和 MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大，可以按照实际情况相应调节 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64，如果负载较大，64也是不够的。这时要显式使用 ThreadLimit指令，它的最大缺省值是20000。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild 值决定的，应该大于等于MaxRequestWorkers。如果负载很大，现有的子进程数不能满足时，控制进程会派生新的子进程。默认最大的子进程总数是16，加大时 也需要显式声明ServerLimit（最大值是20000）。需要注意的是，如果显式声明了ServerLimit，那么它乘以 ThreadsPerChild的值必须大于等于MaxRequestWorkers，而且MaxRequestWorkers必须是ThreadsPerChild的整数倍，否则 Apache将会自动调节到一个相应值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># event MPM</span></div><div class="line">&lt;IfModule mpm_event_module&gt;</div><div class="line">StartServers 3</div><div class="line">MinSpareThreads 75</div><div class="line">MaxSpareThreads 250</div><div class="line">ThreadsPerChild 25</div><div class="line">MaxRequestWorkers 400</div><div class="line">MaxConnectionsPerChild 0</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<ul>
<li>StartServers:初始数量的服务器进程开始</li>
<li>MinSpareThreads:　　最小数量的工作线程,保存备用</li>
<li>MaxSpareThreads:　　最大数量的工作线程,保存备用</li>
<li>ThreadsPerChild:　　固定数量的工作线程在每个服务器进程</li>
<li>MaxRequestWorkers:　　最大数量的工作线程</li>
<li>MaxConnectionsPerChild:　　最大连接数的一个服务器进程服务</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/8. Apache 的 mpm 工作模式/">Apache 的 mpm 工作模式</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 apache 工作模式的命令是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -l</span></div><div class="line">Compiled <span class="keyword">in</span> modules:</div><div class="line"> core.c</div><div class="line"> mod_authn_file.c</div><div class="line"> mod_authn_default.c</div><div class="line"> mod_authz_host.c</div><div class="line"> mod_authz_groupfile.c</div><div class="line"> mod_authz_user.c</div><div class="line"> mod_authz_default.c</div><div class="line"> mod_auth_basic.c</div><div class="line"> mod_include.c</div><div class="line"> mod_filter.c</div><div class="line"> mod_log_config.c</div><div class="line"> mod_env.c</div><div class="line"> mod_setenvif.c</div><div class="line"> mod_version.c</div><div class="line"> prefork.c</div><div class="line"> http_core.c</div><div class="line"> mod_mime.c</div><div class="line"> mod_status.c</div><div class="line"> mod_autoindex.c</div><div class="line"> mod_asis.c</div><div class="line"> mod_cgi.c</div><div class="line"> mod_negotiation.c</div><div class="line"> mod_dir.c</div><div class="line"> mod_actions.c</div><div class="line"> mod_userdir.c</div><div class="line"> mod_alias.c</div><div class="line"> mod_so.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apache 三种工作模式：prefork 、 worker 、 event 。可以看到apache 个工作模式是 prefork.c 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 apache 工作模块的命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -M</span></div><div class="line">Loaded Modules:</div><div class="line"> core_module (static)</div><div class="line"> authn_file_module (static)</div><div class="line"> authn_default_module (static)</div><div class="line"> authz_host_module (static)</div><div class="line"> authz_groupfile_module (static)</div><div class="line"> authz_user_module (static)</div><div class="line"> authz_default_module (static)</div><div class="line"> auth_basic_module (static)</div><div class="line"> include_module (static)</div><div class="line"> filter_module (static)</div><div class="line"> log_config_module (static)</div><div class="line"> env_module (static)</div><div class="line"> setenvif_module (static)</div><div class="line"> version_module (static)</div><div class="line"> mpm_prefork_module (static)</div><div class="line"> http_module (static)</div><div class="line"> mime_module (static)</div><div class="line"> status_module (static)</div><div class="line"> autoindex_module (static)</div><div class="line"> asis_module (static)</div><div class="line"> cgi_module (static)</div><div class="line"> negotiation_module (static)</div><div class="line"> dir_module (static)</div><div class="line"> actions_module (static)</div><div class="line"> userdir_module (static)</div><div class="line"> alias_module (static)</div><div class="line"> so_module (static)</div><div class="line"> deflate_module (shared)</div><div class="line"> expires_module (shared)</div><div class="line"> rewrite_module (shared)</div><div class="line"> php5_module (shared)</div><div class="line">Syntax OK</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过这个命令也可以看到哪些模块是动态，哪些是静态。其中以 mpm_ 开头的就是apache 的 mpm 工作模式，2.2版本的 httpd 默认的 mpm 工作模式为 prefork 。2.4版本的 httpd 默认是 event 工作模式。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/62. apache设置自定义header/">apache设置自定义header</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<ol>
<li>在设置自定义header前，需要先检测一下你的httpd是否加载了mod_headers</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/apache2/bin/apachectl -l</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果，显示有mode_headers.c  则是加载了这个模块，否则就需要重新编译一下了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外，如果你使用的是rpm安装的话，那肯定是已经加载了mod_headers这个模块的。</p>
<ol>
<li>在httpd.conf 中加入</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Header add MyHeader <span class="string">"Hello"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，重启apache就可以了</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;双引号中的内容为自定义内容。当然这里的”MyHeader”也是可以自定义的。</p>
<ol>
<li>测试</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">curl -I http://localhost</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否显示有  MyHeader  “Hello”</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/7. centos 6 编译安装httpd-2.4/">centos 6 编译安装httpd-2.4</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;centos6 yum安装的apr版本已经不适用httpd-2.4版本了。所以，需要源码编译apr以及apr-util</p>
<ol>
<li>下载源码：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/</div><div class="line">wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.12.tar.bz2</div><div class="line"> </div><div class="line">wget http://mirrors.cnnic.cn/apache/apr/apr-1.5.2.tar.bz2</div><div class="line"> </div><div class="line">wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.4.tar.gz</div></pre></td></tr></table></figure>
<ol>
<li>安装apr</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">tar jxvf apr-1.5.2.tar.bz2</div><div class="line"><span class="built_in">cd</span> apr-1.5.2</div><div class="line"> </div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apr</div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<ol>
<li>安装apr-util</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">tar zxvf apr-util-1.5.4.tar.gz</div><div class="line"> </div><div class="line"><span class="built_in">cd</span> apr-util-1.5.4</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apr-util --with-apr=/usr/<span class="built_in">local</span>/apr/</div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<ol>
<li>安装httpd</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">yum install gcc make cmake pcre-devel</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apache2 \</div><div class="line">--with-apr=/usr/<span class="built_in">local</span>/apr \</div><div class="line">--with-apr-util=/usr/<span class="built_in">local</span>/apr-util/</div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/61. apache的proxy代理总访问后端web的第一个虚拟主机/">apache的proxy代理总访问后端web的第一个虚拟主机</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;问题，如题。怎么折腾都是一直访问第一个虚拟主机，后来找到问题所在。是我配置的不对。错误配置是这样的：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">ServerName www.test.com</div><div class="line">CustomLog <span class="string">"/dev/null"</span> combined</div><div class="line">ProxyRequests Off</div><div class="line"> </div><div class="line">Order deny,allow</div><div class="line">Allow from all</div><div class="line"> </div><div class="line">ProxyPass / http://192.168.13.111/</div><div class="line">ProxyPassReverse / 192.168.13.111/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;问题出在了：  ProxyPass / <a href="http://192.168.13.111/" target="_blank" rel="external">http://192.168.13.111/</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要改成这样：  ProxyPass / <a href="http://www.test.com/" target="_blank" rel="external">http://www.test.com/</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以正确的配置是这样的：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">ServerName www.test.com</div><div class="line">CustomLog <span class="string">"/dev/null"</span> combined</div><div class="line">ProxyRequests Off</div><div class="line"> </div><div class="line">Order deny,allow</div><div class="line">Allow from all</div><div class="line"> </div><div class="line">ProxyPass / http://www.test.com/</div><div class="line">ProxyPassReverse / 192.168.13.111/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外需要注意的是，需要在/etc/hosts 中加一条记录  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">192.168.13.111  www.test.com</div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/42">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
